package com.chushouya.order.service.api.impl;

import com.alipay.api.domain.AlipayOpenMiniOrderDeliverySendModel;
import com.alipay.api.domain.DeliveryInfoDTO;
import com.alipay.api.domain.GoodsItemDTO;
import com.general.framework.core.lang.Strings;
import com.chushouya.manager.service.common.AlipayClientCommonService;
import com.chushouya.order.dao.entity.OrderEntity;
import com.chushouya.order.dao.entity.OrderExtendEntity;
import com.chushouya.order.dao.entity.OrderLogisticsEntity;
import com.chushouya.order.dao.entity.OrderProductEntity;
import com.chushouya.order.dao.repository.OrderExtendRepository;
import com.chushouya.order.dao.repository.OrderLogisticsRepository;
import com.chushouya.order.dao.repository.OrderProductRepository;
import com.chushouya.order.dao.repository.OrderRepository;
import com.chushouya.order.dto.admin.orderlogistics.OrderLogisticsQuery;
import com.chushouya.order.service.api.AlipayOpenMiniOrderDeliverySendService;
import com.general.framework.core.exception.Ex;
import com.general.framework.core.lang.Dates;
import com.general.framework.core.lang.Lists;
import com.general.framework.core.lang.Numbers;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class AlipayOpenMiniOrderDeliverySendServiceImpl implements AlipayOpenMiniOrderDeliverySendService {

    @Resource
    private OrderRepository orderRepository;

    @Resource
    private OrderExtendRepository orderExtendRepository;

    @Resource
    private OrderProductRepository orderProductRepository;

    @Resource
    private OrderLogisticsRepository orderLogisticsRepository;

    @Resource
    private AlipayClientCommonService alipayClientCommonService;

    /**
     * 获取订单信息
     * @param orderId
     */
    private OrderEntity getOrder(Long orderId) {
        OrderEntity orderEntity = orderRepository.selectByPrimaryKey(orderId);
        if (Objects.isNull(orderEntity)) {
            throw new RuntimeException("订单不存在");
        }
        return orderEntity;
    }

    /**
     * 订单额外信息
     * @param orderId
     */
    private OrderExtendEntity getOrderExtend(Long orderId) {
        OrderExtendEntity orderExtendEntity = orderExtendRepository.selectByOrderId(orderId);
        if (Objects.isNull(orderExtendEntity)) {
            throw Ex.business("订单扩展信息不存在");
        }
        return orderExtendEntity;
    }

    /**
     * 获取订单产品信息
     * @param orderId
     */
    private List<OrderProductEntity> getOrderProduct(Long orderId) {
        List<OrderProductEntity> orderProductEntities = orderProductRepository.getProductListByOrderId(orderId);
        if (CollectionUtils.isEmpty(orderProductEntities)) {
            throw Ex.business("订单产品信息不存在");
        }
        return orderProductEntities;
    }

    /**
     * 获取订单快递单号
     * @param orderId
     */
    private String getExpressNo(Long orderId) {
        OrderLogisticsQuery query = new OrderLogisticsQuery();
        query.setOrderId(orderId);
        OrderLogisticsEntity orderLogisticsEntity = orderLogisticsRepository.selectOne(query);
        if (Objects.isNull(orderLogisticsEntity)) {
            throw Ex.business("订单物流信息不存在");
        }
        return orderLogisticsEntity.getExpressNo();
    }

    @Override
    public void orderDeliverySend(Long orderId,Long finishAllDelivery) {
        OrderEntity orderEntity = getOrder(orderId);
        OrderExtendEntity orderExtendEntity = getOrderExtend(orderId);
        List<OrderProductEntity> orderProductList = getOrderProduct(orderId);
        String expressNo = getExpressNo(orderId);
        AlipayOpenMiniOrderDeliverySendModel model = new AlipayOpenMiniOrderDeliverySendModel();
        model.setOrderId(orderEntity.getOrderCenterNo());
        model.setOutOrderId(orderEntity.getOrderNo());
        model.setUserId(orderExtendEntity.getOpenId());
        model.setFinishAllDelivery(finishAllDelivery);
        model.setShipDoneTime(Dates.getTimeNowStr());
        model.setDeliveryList(buildDeliveryInfo(orderExtendEntity,orderProductList,expressNo));
        alipayClientCommonService.alipayOpenMiniOrderDeliverySend(model);
    }

    /**
     * 构建支付宝订单发货参数
     */
    private List<DeliveryInfoDTO> buildDeliveryInfo(OrderExtendEntity orderExtendEntity, List<OrderProductEntity> orderProductList, String expressNo) {
        List<DeliveryInfoDTO> deliveryInfoList = Lists.newArrayList();
        orderProductList.forEach(orderProduct -> {
            DeliveryInfoDTO deliveryInfo = new DeliveryInfoDTO();
            deliveryInfo.setDeliveryId("SF");
            deliveryInfo.setWaybillId(expressNo);
            deliveryInfo.setItemInfoList(buildGoodsItem(orderProductList));
            deliveryInfoList.add(deliveryInfo);
        });
        return deliveryInfoList;
    }

    /**
     * 构建支付宝订单发货参数
     */
    private List<GoodsItemDTO> buildGoodsItem(List<OrderProductEntity> orderProductList) {
        List<GoodsItemDTO> goodsItemList = Lists.newArrayList();
        orderProductList.forEach(orderProduct -> {
            GoodsItemDTO goodsItem = new GoodsItemDTO();
            goodsItem.setGoodsId(Strings.format("{}",orderProduct.getProductId()));
            goodsItem.setOutItemId(Strings.format("{}",orderProduct.getProductId()));
            goodsItem.setOutSkuId(Strings.format("{}",orderProduct.getProductId()));
            goodsItem.setItemCnt(1L);
            goodsItemList.add(goodsItem);
        });
        return goodsItemList;
    }

}
